<template>
  <!-- 头部导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="leaveBgHighlight" @mouseenter="enterList">
        <h2 class="all">全部商品分类</h2>
        <!-- 菜单盒子 -->
        <transition name="sort">
          <div class="sort" v-show="show">
            <div
              class="item"
              v-for="(c1, index) in categoryLists"
              :key="c1.categoryId"
              :class="{ cur: currentList == index }"
              @click="toSearch($event)"
            >
              <!-- 一级标题 -->
              <h3 @mouseenter="hoverBgHighlight(index)">
                <a
                  :data-categoryName="c1.categoryName"
                  :data-category1Id="c1.categoryId"
                >
                  {{ c1.categoryName }}
                </a>
              </h3>
              <div
                class="item-list"
                :style="{ display: currentList == index ? 'block' : 'none' }"
              >
                <!-- display页面 -->
                <div
                  class="subitem"
                  v-for="c2 in c1.categoryChild"
                  :key="c2.categoryId"
                >
                  <dl class="fore">
                    <dt>
                      <a
                        :data-categoryName="c2.categoryName"
                        :data-category2Id="c2.categoryId"
                        >{{ c2.categoryName }}</a
                      >
                    </dt>
                    <dd>
                      <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                        <a
                          :data-categoryName="c3.categoryName"
                          :data-category3Id="c3.categoryId"
                          >{{ c3.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
// import _ from "lodash";
import throttle from "lodash/throttle";
export default {
  name: "header-nav-vue",
  //展示数据
  data() {
    return {
      currentList: -1,
      show: true,
    };
  },
  computed: {
    ...mapState({
      categoryLists: (state) => {
        //因为没有命名空间，所以这个state是全部的state
        return state.home.categoryLists.slice(0, 15);
      },
    }),
  },
  methods: {
    /*  hoverBgHighlight(index) {
      this.currentList = index;
      // console.log("我进入" + index)
    }, */
    hoverBgHighlight: throttle(function (index) {
      //节流
      this.currentList = index;
    }, 50),
    leaveBgHighlight() {
      this.currentList = -1;
      if (this.$route.path != "/home") {
        //非首页都要隐藏
        this.show = false;
      }
    },
    enterList() {
      if (this.$route.path != "/home") {
        //非首页都要显示
        this.show = true;
      }
    },
    toSearch(event) {
      let element = event.target; //子元素
      let { categoryname, category1id, category2id, category3id } =
        element.dataset; //获取所有自定义属性map集合
      let location = { name: "search" };
      let query = { categoryName: categoryname };
      if (categoryname) {
        //如果categoryname，那么一定是a标签，就可以跳转啦
        if (category1id) {
          //一级标题
          query.category1Id = category1id;
        } else if (category2id) {
          //二级
          query.category2Id = category2id;
        } else {
          //三级
          query.category3Id = category3id;
        }
        //判断是否有搜索框的params参数，在请求中加入params
        if (this.$route.params) {
          location.params = this.$route.params;
        }
      }
      location.query = query;
      this.$router.push(location);
    },
  },
  //获取三级列表请求的放在app.vue
  created() {
    if (this.$route.path != "/home") {
      //非首页都要隐藏
      this.show = false;
    }
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #ea4a36;
  .container {
    display: flex;
    position: relative;
    width: 1200px;
    margin: 0 auto;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }
    .nav a {
      display: inline-block;
      padding: 0 22px;
      line-height: 45px;
      text-align: center;
      text-decoration: none;
      font-size: 14px;
      color: #333;

      &:hover {
        background-color: #cccccc73;
      }
    }
    .sort {
      position: absolute;
      top: 45px;
      left: 0;
      width: 210px;
      height: 461px;
      background-color: #fafafa;
      z-index: 999;

      .item {
        h3 {
          line-height: 30px;
          font-size: 14px;
          font-weight: 400;
          padding-left: 20px;
          margin: 0;

          a {
            display: inline-block;
            width: 100%;
            text-decoration: none;
            color: #333;
          }
        }

        .item-list {
          // display: none;
          position: absolute;
          top: 0;
          left: 210px;
          width: 734px;
          min-height: 461px;
          background-color: #f7f7f7f7;
          border: 1px solid #ddd;
          z-index: 999;

          .subitem {
            width: 650px;
            padding: 0 4px 0 8px;

            dl {
              // border-top: 1px solid #eee;
              padding: 6px 0;
              overflow: hidden; //清除浮动
              zoom: 1;

              dt {
                float: left;
                width: 54px;
                line-height: 22px;
                text-align: right;
                padding: 3px 6px 0 0;
                font-weight: 700;
              }

              dd {
                float: left;
                width: 415px;
                padding: 3px 0 0;
                overflow: hidden;

                em {
                  display: inline-block;
                  height: 14px;
                  line-height: 14px;
                  margin-top: 5px;
                  padding: 0 8px;
                  border-left: 1px solid #ccc;
                }
              }
            }
          }
        }

        /* &:hover {
          background-color: #cccccc73;
          border-bottom: 1px solid #b7b7b7;
          cursor: pointer;
        } */
        /* &:hover .item-list {
          display: block;
        } */
      }

      .cur {
        background-color: #cccccc73;
        border-bottom: 1px solid #b7b7b7;
        cursor: pointer;
      }
      // 过渡动画
      &-enter {
        //进入起点
        opacity: 0;
      }
      &-enter-active {
        transition: all 0.3s linear;
      }
      &-enter-to {
        opacity: 1;
      }
      &-leave {
        opacity: 1;
      }
      &-leave-active {
        transition: all 0.3s linear;
      }
      &-leave-to {
        opacity: 0;
      }
    }
  }
}
</style>